package com.wk.kxexamadminister.controller;

import com.wk.kxexamadminister.entity.Judgment;
import com.wk.kxexamadminister.entity.ManyChoice;
import com.wk.kxexamadminister.entity.SingleChoice;
import com.wk.kxexamadminister.entity.fillBlanks;
import com.wk.kxexamadminister.mapper.JudgmentMapper;
import com.wk.kxexamadminister.mapper.ManyChoiceMapper;
import com.wk.kxexamadminister.mapper.SingleMapper;
import com.wk.kxexamadminister.mapper.fillBlanksMapper;
import com.wk.kxexamadminister.utils.uploadUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ExcelAdd {

    @Autowired
    private ManyChoiceMapper manyChoiceMapper;
    @Autowired
    private JudgmentMapper judgmentMapper;
    @Autowired
    private SingleMapper singleMapper;
    @Autowired
    private fillBlanksMapper fillBlanksMapper;


    public static final int CELL_TYPE = 1;

    @PostMapping("/addExcelManyChoice")
    @ResponseBody
    public String addExcelManyChoice(MultipartFile fileName){
        try {
            String file = uploadUtils.uploadFile(fileName);
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            int numberOfSheets = workbook.getNumberOfSheets();

            ManyChoice manyChoice = new ManyChoice();
            Judgment judgment = new Judgment();
            SingleChoice singleChoice = new SingleChoice();
            fillBlanks fillBlanks = new fillBlanks();


            int lastRowNum;
            XSSFRow row;
            XSSFSheet  sheetAt;
            String sheetName;
            int k;

            for(int i=0; i<numberOfSheets;i++){
                sheetAt = workbook.getSheetAt(i);
                sheetName = sheetAt.getSheetName();
                lastRowNum = sheetAt.getLastRowNum() + 1;
                if("多选题".equals(sheetName)){
                    for (k=1;k<lastRowNum;k++) {
                        row = sheetAt.getRow(k);
                        Cell cell = row.getCell(k);
                        if (cell.getStringCellValue() != null && !cell.toString().trim().equals("")) {
                            manyChoice.setTitleId(new Double(row.getCell(0).getNumericCellValue()).intValue());
                            manyChoice.setTitle(row.getCell(1).getStringCellValue());
                            manyChoice.setDescription(row.getCell(2).getStringCellValue());
                            manyChoice.setAnswer(row.getCell(3).getStringCellValue());
                            manyChoice.setScore(new Double(row.getCell(4).getNumericCellValue()).intValue());
                            manyChoice.setCId(new Double(row.getCell(5).getNumericCellValue()).intValue());
                            manyChoice.setIId(new Double(row.getCell(6).getNumericCellValue()).intValue());
                            manyChoiceMapper.insert(manyChoice);
                        }
                    }
                }
                if("判断题".equals(sheetName)){
                    for(k=1;k<lastRowNum;k++) {
                        row = sheetAt.getRow(k);
                        XSSFCell cell = row.getCell(k);
                        if (cell.getStringCellValue() != null && !cell.toString().trim().equals("")) {
                            judgment.setTitleId(new Double(row.getCell(0).getNumericCellValue()).intValue());
                            judgment.setTitle(row.getCell(1).getStringCellValue());
                            judgment.setAnswer(row.getCell(2).getStringCellValue());
                            judgment.setScore(new Double(row.getCell(3).getNumericCellValue()).intValue());
                            judgment.setCId(new Double(row.getCell(4).getNumericCellValue()).intValue());
                            judgment.setIId(new Double(row.getCell(5).getNumericCellValue()).intValue());
                            judgmentMapper.insert(judgment);
                        }
                    }
                }
                if("单选题".equals(sheetName)){
                    for (k=1;k<lastRowNum;k++) {
                        row = sheetAt.getRow(k);
                        XSSFCell cell = row.getCell(k);
                        if (cell.getStringCellValue() != null && !cell.toString().trim().equals("")) {
                            singleChoice.setTitleId(new Double(row.getCell(0).getNumericCellValue()).intValue());
                            singleChoice.setTitle(row.getCell(1).getStringCellValue());
                            singleChoice.setDescription(row.getCell(2).getStringCellValue());
                            singleChoice.setAnswer(row.getCell(3).getStringCellValue());
                            singleChoice.setScore(new Double(row.getCell(4).getNumericCellValue()).intValue());
                            singleChoice.setCId(new Double(row.getCell(5).getNumericCellValue()).intValue());
                            singleChoice.setIId(new Double(row.getCell(6).getNumericCellValue()).intValue());
                            singleMapper.insert(singleChoice);
                        }
                    }
                }
                if("填空题".equals(sheetName)){
                    for(k=1;k<lastRowNum;k++) {
                        row = sheetAt.getRow(k);
                        XSSFCell cell = row.getCell(k);
                        if (cell.getStringCellValue() != null && !cell.toString().trim().equals("")) {
                            fillBlanks.setTitleId(new Double(row.getCell(0).getNumericCellValue()).intValue());
                            fillBlanks.setTitle(row.getCell(1).getStringCellValue());
                            fillBlanks.setAnswer(row.getCell(2).getStringCellValue());
                            fillBlanks.setScore(new Double(row.getCell(3).getNumericCellValue()).intValue());
                            fillBlanks.setCId(new Double(row.getCell(4).getNumericCellValue()).intValue());
                            fillBlanks.setIId(new Double(row.getCell(5).getNumericCellValue()).intValue());
                            fillBlanksMapper.insert(fillBlanks);
                        }
                    }
                }
            }



            return "Success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }

}
